package net.dopan.pigframe.core.base.entity;

import com.baomidou.mybatisplus.activerecord.Model;
import com.baomidou.mybatisplus.annotations.TableField;
import lombok.Data;
import net.dopan.pigframe.core.utils.Constant;
import net.dopan.pigframe.core.utils.UserUtils;

import java.io.Serializable;
import java.util.Date;

/**
 * @Package: net.dopan.pigframe.core.base.entity
 * @Title: BaseEntity
 * @Description: 基本Entity
 * @author: 刘宽
 * @date: 2018/8/20 16:15
 */
@Data
public abstract class BaseEntity<T extends Model> extends Model<T> implements Serializable {

    private static final long serialVersionUID = -2313980485913569220L;

    /**
     * 状态位
     */
    protected String status = Constant.NOT_DELETED;

    /**
     * 创建者
     */
    @TableField("create_by")
    protected String createBy;

    /**
     * 创建时间
     */
    @TableField("create_time")
    protected Date createTime;

    /**
     * 更新者
     */
    @TableField("update_by")
    protected String updateBy;

    /**
     * 更新时间
     */
    @TableField("update_time")
    protected Date updateTime;

    /**
     * 保留字1
     */
    protected String attr1;

    /**
     * 保留字2
     */
    protected String attr2;

    public <T extends BaseEntity> T create(){
        Date date = new Date();
        setStatus(Constant.NOT_DELETED);
        setCreateBy(UserUtils.getUser());
        setCreateTime(date);
        setUpdateTime(date);
        return (T) this;
    }

    public <T extends BaseEntity> T update(){
        setUpdateBy(UserUtils.getUser());
        setUpdateTime(new Date());
        return (T) this;
    }
}
